package com.lanfan.controller.admin;

import com.lanfan.entity.ResponseResult;
import com.lanfan.entity.Users;
import com.lanfan.entity.UsersPage;
import com.lanfan.service.UsersService;
import com.lanfan.utils.OSSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @program: lanfan
 * @description:
 * @author: ZQW
 * @create: 2024-10-03 17:42
 * @version: 1.0
 **/
@RestController("adminUsersController")
@RequestMapping("/admin/users")
public class UsersController {
    @Resource
    private UsersService usersService;

    @Resource
    private OSSUtil ossUtil;

    @PostMapping("/save")
    public ResponseResult save (@RequestBody Users users) {
        usersService.save(users);
        ResponseResult responseResult=new ResponseResult();
        return responseResult;
    }

    @DeleteMapping("/delete/{userid}")
    public ResponseResult delete(@PathVariable Integer userid){
        usersService.delete(userid);
        ResponseResult responseResult=new ResponseResult();
        return responseResult;
    }

    @PostMapping("/search")
    public ResponseResult search(@RequestBody UsersPage usersPage){


        /*
        引用传递  2个引用变量操作堆中同一个对象
         */
        ResponseResult responseResult=new ResponseResult();
        usersService.search(usersPage);
        responseResult.setDate(usersPage);
        return responseResult;
    }

    @PostMapping("/findByPhone")
    public ResponseResult findByPhone(@RequestBody Users users){
        String phone = users.getPhone();
        Users user = usersService.findByPhone(phone);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setDate(user);
        return responseResult;
    }

    @RequestMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        String upload = ossUtil.upload(file);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setDate(upload);// data:"https://xxxxx/1.jpg"
        return responseResult;
    }
}
